package com.example.ice.fittime.adapter.user;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.ice.fittime.R;
import com.example.ice.fittime.entry.user.ArticleBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ice on 2016/10/13.
 */

public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.VH> {

    private final LayoutInflater from;
    private Context context;
    private List<ArticleBean> allData;

    public ArticleAdapter(Context context) {
        this.context = context;
        allData = new ArrayList<>();
        from = LayoutInflater.from(context);
    }

    public void addAll(List<ArticleBean> newData) {
        allData.addAll(newData);
        notifyDataSetChanged();
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        return new VH(from.inflate(R.layout.rv_article,parent,false));
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        ArticleBean bean = allData.get(position);
        holder.title.setText(bean.getTitle());
        holder.category.setText(bean.getCategory());
        holder.description.setText(bean.getDescription());
        Glide.with(context).load(bean.getImage_url()).into(holder.image_url);
    }

    @Override
    public int getItemCount() {
        return allData == null ? 0 : allData.size();
    }

    public class VH extends RecyclerView.ViewHolder {

        private final TextView title;
        private final TextView category;
        private final TextView description;
        private final ImageView image_url;

        public VH(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            description = ((TextView) itemView.findViewById(R.id.description));
            category = ((TextView) itemView.findViewById(R.id.category));
            image_url = ((ImageView) itemView.findViewById(R.id.image_url));
        }
    }
}
